package com.cyl.XmlDemo.web;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyl.XmlDemo.entity.DataResult;
import com.cyl.XmlDemo.entity.Friend;
import com.cyl.XmlDemo.entity.Group;
import com.cyl.XmlDemo.entity.RecordNote;
import com.cyl.XmlDemo.entity.ShiroPrincipal;
import com.cyl.XmlDemo.entity.User;
import com.cyl.XmlDemo.service.FriendsService;
import com.cyl.XmlDemo.service.GroupService;
import com.cyl.XmlDemo.utils.ShiroUtils;


/**
 * 联系人和群组管理控制器
 * @author tyuio
 *
 */
@Controller
@RequestMapping("/mana")
public class ManaController {
	
	private final Logger log = LoggerFactory.getLogger(ManaController.class);

	@Autowired
	private GroupService groupService;
	
	@Autowired
	private FriendsService friendsService;
	
	@RequestMapping("/recordList")
	@ResponseBody
	public DataResult getRecordList(DataResult dr){
		log.debug("成功触发 联系人记录");
		ShiroPrincipal sp = ShiroUtils.getPrincipal();
		int uid = ShiroUtils.getPrincipal().getUser().getId();
		//获取好友列表
		List<Friend> friendList = friendsService.getFriendsByUid(uid);
		//获取群组列表
		List<Group> groupList = groupService.getGroupByUid(uid);
		
		RecordNote rn = new RecordNote();
		rn.setFriends(friendList);
		rn.setGroups(groupList);
		dr.setCode("success");
		dr.setData(rn);
		return dr;
	}
	
	//添加联系人
	@RequestMapping(value="/addfriend")
	@ResponseBody
	public DataResult addfriend(DataResult dr,Friend friend){
		User user = ShiroUtils.getPrincipal().getUser();
		friend.setRecord(user.getId());
		String code = friendsService.addFriend(friend);
		dr.setCode(code);
		return dr;
	}
	
	//删除好友
	@RequestMapping(value="/delfriends")
	public DataResult delfriends(String friendId,DataResult dr){
		return dr;
	}
	
	//加入群组
	@RequestMapping(value="/joingroup")
	public DataResult joingroup(DataResult dr){
		//向群组发送加入申请，若离线则先存在数据库
		return dr;
	}
	
	
	//退出群组
	@RequestMapping(value="/delgroup")
	public DataResult delgroup(String groupId,DataResult dr){
		return dr;
	}
	
	//添加好友
	@RequestMapping(value="/bestFriend")
	public DataResult bestFriend(DataResult dr){
		//对方同意添加好友后，需要向申请认发送信息
		return dr;
	}
		
	//加入群组
	@RequestMapping(value="/groupMember")
	public DataResult groupMember(DataResult dr){
		//群主同意加入后，需要向申请人发送信息
		return dr;
	}
}
